package com.middleware.redis.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.middleware.redis.util.RedisUtil;
import com.middleware.redis.util.RedisUtil.KeyOps;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiongbangwen
 * @date 2022-01-18 18:35
 */
@RestController
@RequestMapping(path = "/keyOps")
public class KeyOpsController {

    @Autowired
    RedisUtil redisUtil;

    @GetMapping("/expire")
    public String expire(@RequestParam(value = "key", defaultValue = "redis:test:expire") String key) {
        return String.valueOf(KeyOps.expire(key, 10, TimeUnit.MINUTES));
    }

    @GetMapping("/expireAt")
    public String expireAt(@RequestParam(value = "key", defaultValue = "redis:test:expire") String key) {
        return String.valueOf(KeyOps.expireAt(key, DateUtil.parseDatetime("2022-03-03 11:20:00")));
    }

    @GetMapping("/delete")
    public String delete(@RequestParam(value = "key", defaultValue = "redis:test:expire") String key) {
        return String.valueOf(KeyOps.delete(key));
    }

    @GetMapping("/batchDelete")
    public String batchDelete(@RequestParam(value = "keys", defaultValue = "redis:test:expire") List<String> keys) {
        return String.valueOf(KeyOps.delete(keys));
    }

    @GetMapping("/hasKey")
    public String hasKey(@RequestParam(value = "key", defaultValue = "redis:test:expire") String key) {
        return String.valueOf(KeyOps.hasKey(key));
    }

    @GetMapping("/getExpire")
    public String getExpire(@RequestParam(value = "key", defaultValue = "redis:test:expire") String key) {
        return String.valueOf(KeyOps.getExpire(key));
    }

    @GetMapping("/getExpireByTimeUnit")
    public String getExpireByTimeUnit(@RequestParam(value = "key", defaultValue = "redis:test:expire") String key) {
        return String.valueOf(KeyOps.getExpire(key, TimeUnit.MINUTES));
    }

    @GetMapping("/rename")
    public String rename(@RequestParam(value = "key", defaultValue = "redis:test:expire") String key) {
        KeyOps.rename(key, "redis:test:expire:new");
        return "success";
    }

    @GetMapping("/renameIfAbsent")
    public String renameIfAbsent(@RequestParam(value = "key", defaultValue = "redis:test:expire") String key) {
        return String.valueOf(KeyOps.renameIfAbsent(key, "redis:test:expire:new"));
    }

    @GetMapping("/dump")
    public String dump(@RequestParam(value = "key", defaultValue = "redis:test:expire") String key) {
        return String.valueOf(KeyOps.dump(key));
    }

    @GetMapping("/restore")
    public String restore(@RequestParam(value = "key", defaultValue = "redis:test:expire") String key) {
        KeyOps.restore(key,KeyOps.dump(key),10,TimeUnit.MINUTES);
        return "success";
    }

    @GetMapping("/randomKey")
    public String randomKey() {
        return KeyOps.randomKey();
    }

    @GetMapping("/move")
    public String move(@RequestParam(value = "key", defaultValue = "redis:test:expire") String key) {
        return String.valueOf(KeyOps.move(key, 1));
    }

    @GetMapping("/persist")
    public String persist(@RequestParam(value = "key", defaultValue = "redis:test:expire") String key) {
        return String.valueOf(KeyOps.persist(key));
    }

    @GetMapping("/type")
    public String type(@RequestParam(value = "key", defaultValue = "redis:test:expire") String key) {
        return String.valueOf(KeyOps.type(key));
    }
}
